package com.hh.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hh.entity.Order;
import com.hh.service.OrderService;
import com.hh.vo.OrderConfirmResult;
import com.hh.vo.OrderParam;
import com.hh.vo.OrderStatusResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * <p>
 * 订单表 前端控制器
 * </p>
 *
 * @author 辉辉
 * @since 2022-08-21
 */
@RestController
@Api(tags = "订单管理接口")
public class OrderController {
    @Resource
    private OrderService orderService;

    @GetMapping("/p/myOrder/orderCount")
    @ApiOperation("查询当前用户的订单数量")
    public ResponseEntity<OrderStatusResult> findOrderCount() {
        String openId = SecurityContextHolder.getContext().getAuthentication().getPrincipal().toString();
        OrderStatusResult orderStatusResult = orderService.findOrderCount(openId);
        return ResponseEntity.ok(orderStatusResult);
    }

    @GetMapping("/p/myOrder/myOrder")
    @ApiOperation("分页查询当前用户的订单")
    public ResponseEntity<Page<Order>> myOrderPage(Page<Order> page, Order order) {
        String openId = SecurityContextHolder.getContext().getAuthentication().getPrincipal().toString();
        order.setUserId(openId);
        Page<Order> orderPage = orderService.findOrderPage(page, order);
        return ResponseEntity.ok(orderPage);
    }

    /**
     * controller 写接口信息（参数校验） 不写业务
     * service只写业务
     *
     * @param orderParam
     * @return
     */
    @PostMapping("/p/order/confirm")
    @ApiOperation("订单的确认接口")
    public ResponseEntity<OrderConfirmResult> confirm(@RequestBody OrderParam orderParam) {
        String openId = SecurityContextHolder.getContext().getAuthentication().getPrincipal().toString();
        OrderConfirmResult orderConfirmResult = orderService.confirm(openId, orderParam);
        return ResponseEntity.ok(orderConfirmResult);
    }

    /**
     * 返回的订单号是String类型 但是ajax做数据处理的时候会把String转换成大的整型 就会出现精度的缺失
     * 返回的时候不要返回纯数字
     * @param orderConfirmResult
     * @return
     */
    @PostMapping("/p/order/submit")
    @ApiOperation("订单的提交")
    public ResponseEntity<String> myOrderSubmit(@RequestBody OrderConfirmResult orderConfirmResult) {
        String openId = SecurityContextHolder.getContext().getAuthentication().getPrincipal().toString();
        String orderNum = orderService.submit(openId, orderConfirmResult);
        return ResponseEntity.ok("orderNum:" + orderNum);
    }
}

